package online.flowerinsnow.minecraft.bukkit.manhunt.core.scoreboard.impl;

import online.flowerinsnow.minecraft.bukkit.manhunt.manager.ITeamManager;
import online.flowerinsnow.minecraft.bukkit.manhunt.runnable.DelayStartRunnable;
import online.flowerinsnow.minecraft.bukkit.manhunt.runnable.GameTimerRunnable;
import online.flowerinsnow.minecraft.bukkit.manhunt.util.MessageUtils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.scoreboard.*;
import online.flowerinsnow.minecraft.bukkit.manhunt.Manhunt;
import online.flowerinsnow.minecraft.bukkit.manhunt.core.PlayerTeam;
import online.flowerinsnow.minecraft.bukkit.manhunt.core.scoreboard.IPlayerScoreboard;

import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

public class PlayerScoreboardImpl implements IPlayerScoreboard {
    private final Set<Integer> lineSet = new HashSet<>();
    private final Scoreboard sb = Objects.requireNonNull(Bukkit.getScoreboardManager()).getNewScoreboard();
    public final Player player;
    public final Team hunter = sb.registerNewTeam("team-hunter");
    public final Team runner = sb.registerNewTeam("team-runner");
    public final Team spectator = sb.registerNewTeam("team-spectator");
    public final Objective objective = sb.registerNewObjective("side", "dummy", MessageUtils.parseColor("&e&lManhunt"));

    public PlayerScoreboardImpl(Player player) {
        this.player = player;
        initTeams();
        initObjective();
        player.setScoreboard(sb);
    }

    private void initTeams() {
        hunter.setColor(ChatColor.RED);
        hunter.setCanSeeFriendlyInvisibles(false);
        hunter.setPrefix(MessageUtils.parseColor("&c[猎人]"));
        runner.setColor(ChatColor.AQUA);
        runner.setCanSeeFriendlyInvisibles(false);
        runner.setPrefix(MessageUtils.parseColor("&b[逃跑者]"));
        spectator.setColor(ChatColor.GRAY);
        spectator.setCanSeeFriendlyInvisibles(false);
        spectator.setPrefix(MessageUtils.parseColor("&7[观察者]"));
    }

    private void initObjective() {
        objective.setDisplaySlot(DisplaySlot.SIDEBAR);
        setTeamInfo();
        setTimeInfo();
        setScoreboardLine(8, "§b§m--------------------");
        setScoreboardLine(5, "");
        setScoreboardLine(2, "§b§m--------------------");
        setScoreboardLine(1, "§ev4.flowerinsnow.online");
        /*
                 Manhunt
        ====================    8
        时间: 00:00:00           7
        猎人出动时间: 10s         6
                                5
        (0) 猎人                 4
        (0) 逃跑者               3
        ====================    2
        flowerinsnow.online     1
         */
    }

    @Override
    public void setScoreboardLine(int line, String text) {
        if (text == null) {
            sb.resetScores("§" + (char) line);
            lineSet.remove(line);
        } else {
            Team team = sb.getTeam("sb-" + line);
            if (team == null) {
                team = sb.registerNewTeam("sb-" + line);
                team.addEntry("§" + (char) line);
            }
            String prefix;
            String suffix;
            if (text.length() > 64) {
                prefix = text.substring(64);
                if (prefix.endsWith("§")) {
                    prefix = prefix.substring(0, 63);
                    suffix = text.substring(63);
                } else {
                    suffix = ChatColor.getLastColors(prefix) + text.substring(64);
                }
            } else {
                prefix = text;
                suffix = "";
            }
            team.setPrefix(prefix);
            team.setSuffix(suffix);
            if (lineSet.add(line)) {
                objective.getScore("§" + (char) line).setScore(line);
            }
        }
    }

    @Override
    public void joinHunter(String entry) {
        hunter.addEntry(entry);
    }

    @Override
    public void joinRunner(String entry) {
        runner.addEntry(entry);
    }

    @Override
    public void joinSpectator(String entry) {
        spectator.addEntry(entry);
    }

    @Override
    public void unJoin(String entry) {
        Team team = sb.getEntryTeam(entry);
        if (team != null) {
            team.removeEntry(entry);
        }
    }

    @Override
    public boolean isLineSet(int line) {
        return lineSet.contains(line);
    }

    @Override
    public Player getPlayer() {
        return player;
    }

    @Override
    public void setTeamInfo() {
        ITeamManager teamManager = Manhunt.getTeamManager();
        PlayerTeam team = teamManager.getPlayerTeam(player);
        String var_4_c = "§b(" + teamManager.getRunners().size() + ") 逃跑者";
        final String val_3_d = " §7(你)";
        if (team == PlayerTeam.RUNNER) {
            var_4_c += val_3_d;
        }
        setScoreboardLine(3, var_4_c);

        var_4_c = "§c(" + Manhunt.getTeamManager().getHunters().size() + ") 猎人";
        if (team == PlayerTeam.HUNTER) {
            var_4_c += val_3_d;
        }
        setScoreboardLine(4, var_4_c);

        Bukkit.getOnlinePlayers().forEach(p -> {
            PlayerTeam t = teamManager.getPlayerTeam(p);
            if (t == null) {
                unJoin(p.getName());
                return;
            }
            switch (t) {
                case RUNNER -> joinRunner(p.getName());
                case HUNTER -> joinHunter(p.getName());
                case SPECTATOR -> joinSpectator(p.getName());
            }
        });
    }

    @Override
    public void setTimeInfo() {
        setScoreboardLine(7, "§e时间: " + GameTimerRunnable.getStringTime());
    }

    @Override
    public void setHunterStartInfo() {
        if (DelayStartRunnable.timer == -1) {
            setScoreboardLine(6, null);
            return;
        }
        setScoreboardLine(6, "§c猎人出动时间: §e" + DelayStartRunnable.timer + "s");
    }
}
